package com.su.controller;


import com.amazonaws.services.s3.model.ObjectMetadata;
import com.su.file.util.AwzS3Util;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("awz/s3")
public class S3Controller {


    @PostMapping(value = "/uploadObject")
    public ResponseEntity uploadObject(MultipartFile file, String bucket) {
        try {
            AwzS3Util.upload(file, bucket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity("上传成功!", HttpStatus.OK);
    }

    @PostMapping(value = "/downloadObject")
    public ResponseEntity downloadObject(String bucket, String fileName) {
        ResponseEntity<byte[]> download = null;
        try {
            download = AwzS3Util.downloadByName(bucket, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return download;
    }

    @PostMapping(value = "/getFileInfo")
    public ObjectMetadata getFileInfo(String bucket, String fileName) {
        return AwzS3Util.getFileInfo(bucket, fileName);
    }

}
